package momo.app.structure;

import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SessionTracker
{
	private Log log = LogFactory.getLog(SessionTracker.class);
	private static SessionTracker tracker;
	private int sid;
	
	private SessionTracker()
	{
		sid = 0;
	}
	
	static public SessionTracker getInstance()
	{
		if ( tracker == null )
			tracker = new SessionTracker();
		return tracker;
	}
	
	synchronized public boolean compareAndSet(int sid)
	{
		if ( log.isDebugEnabled() )
			log.debug("my SID: " + this.sid + ", compare SID: " + sid);
		
		if ( sid > this.sid )
		{
			this.sid = sid;
			return true;
		}
		return false;
	}
	
	synchronized public void setSID(int sid)
	{
		this.sid = sid;
	}
	
	synchronized public int getSID()
	{
		return sid;
	}
	
	synchronized public int setAndGetGeneratedSID() 
	{
		setSID( getGeneratedSID() );
		return getSID();
	}
	
	public int getGeneratedSID()
	{
		Random rand = new Random();
		return rand.nextInt();
	}
}//end of class SessionTracker
